package pers.cocoadel.springcloud.cloudgatewaygateway9527;

import com.netflix.client.http.HttpRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.cloud.gateway.filter.GatewayFilterChain;
import org.springframework.cloud.gateway.filter.GlobalFilter;
import org.springframework.core.Ordered;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Component;
import org.springframework.web.server.ServerWebExchange;
import reactor.core.publisher.Mono;

/**
 * @author ZhenWuWang
 */
@Component
@Slf4j
public class MyCustomFilter implements GlobalFilter, Ordered
{
    @Override
    public Mono<Void> filter(ServerWebExchange exchange, GatewayFilterChain chain)
    {
        String username = exchange.getRequest().getQueryParams().getFirst("username");
        if ("rwby".equals(username))
        {
            log.info(String.format("用户名%s匹配正确，登录成功", username));
            return chain.filter(exchange);
        } else
        {
            exchange.getResponse().setStatusCode(HttpStatus.UNAUTHORIZED);
            return exchange.getResponse().setComplete();
        }
    }

    @Override
    public int getOrder()
    {
        return 0;
    }
}
